# 在子类中，你想要扩展定义在父类中的property 的功能

class Person:
    def __init__(self, name):
        self.name = name

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        if not isinstance(value, str):
            raise TypeError('Excepted a string')
        self._name = value

    @name.deleter
    def name(self):
        raise AttributeError("Can't delete attribute")

class SubPerson(Person):
    @property
    def name(self):
        print('Getting name')
        return super().name

    # 为了委托给之前定义的 setter 方法，需要将控制权传递给之前定义的 name 属性的 __set__() 方法。
    # 不过，获取这个方法的唯一途径是使用类变量而不是实例变量来访问它。
    @name.setter
    def name(self, value):
        print('Setting name to', value)
        super(SubPerson, SubPerson).name.__set__(self, value)

    @name.deleter
    def name(self):
        print('Deleting name')
        super(SubPerson, SubPerson).name.__delete__(self)

s = SubPerson('Guido')
print(s.name)
s.name = 'Larry'
try:
    s.name = 42
except Exception as e:
    print(e)

# 只修改property的一个方法，只使用property是不行的
class SubPerson2(Person):
    @Person.name.getter
    def name(self):
        print('Getting name')
        return super().name

    # @Person.name.setter
    # def name(self, value):
    #     print('Setting name to', value)
    #     super(SubPerson2, SubPerson2).name.__set__(self, value)

    # @Person.name.deleter
    # def name(self):
    #     print('Deleting name')
    #     super(SubPerson2, SubPerson2).name.__delete__(self)

s2 = SubPerson2('Xu')
print(s2.name)
